const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    default_img: {},
    express_info: {}, //订单物流信息
    packet_info: {}, //包裹物流信息
    express: {} //快递物流信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let order_id = options.order_id;
    that.setData({
      order_id: order_id
    })
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 加载基本信息
   */
  loadInfo: function() {
    let that = this;
    let order_id = that.data.order_id;

    app.sendRequest({
      url: 'System.Order.logistics',
      data: {
        order_id: order_id
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          let express_info = data;
          let packet_info = app.checkArray(express_info.goods_packet_list) ? express_info.goods_packet_list : [];

          for (let index in packet_info) {
            if (index == 0) {
              packet_info[index].status = 1
            } else {
              packet_info[index].status = 0
            }
            //图片处理
            for (let key in packet_info[index].order_goods_list) {
              if (packet_info[index].order_goods_list[key].picture_info != undefined &&
                packet_info[index].order_goods_list[key].picture_info != null) {
                let img = packet_info[index].order_goods_list[key].picture_info.pic_cover_micro;
                packet_info[index].order_goods_list[key].picture_info.pic_cover_micro = app.IMG(img);
              } else {
                packet_info[index].order_goods_list[key].picture_info = {};
                packet_info[index].order_goods_list[key].picture_info.pic_cover_micro = '';
              }
            }
          }
          express_info.goods_packet_list = packet_info;
          that.setData({
            express_info: express_info,
            packet_info: packet_info[0]
          })

          that.getExpressInfo(that, packet_info[0].express_id);

        } else {
          app.showBox(that, res.message);
        }
        console.log(res)
      }
    });
  },

  /**
   * 图片加载失败
   */
  errorImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let express_info = that.data.express_info;
    let default_img = that.data.default_img;
    let parm = {};
    let img = express_info[index].picture_info.pic_cover_micro;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) == -1) {
        let parm_key = "express_info[" + index + "].picture_info.pic_cover_micro";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 查询物流信息
   */
  getExpressInfo: function(that, express_id) {
    app.sendRequest({
      url: 'System.Order.orderExpressMessageList',
      data: {
        express_id: express_id
      },
      success: function(res) {
        if (res.code == 0) {
          let express = app.checkEmpty(res.data, {});
          if (!express.Success) {
            express = -1;
          }
          that.setData({
            express: express
          })
          console.log(express);
        } else {
          app.showBox(that, res.message);
        }
      }
    });
  },

  /**
   * 导航选择
   */
  navClick: function(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let express_info = that.data.express_info;
    let packet_info = express_info.goods_packet_list;

    for (let key in packet_info) {
      packet_info[key].status = 0;
    }

    packet_info[index].status = 1;

    that.setData({
      express_info: express_info,
      packet_info: packet_info[index],
    })

    that.getExpressInfo(that, packet_info[index].express_id);
  },
})